package com.jl.jlapp.mvp.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.jl.jlapp.R;
import com.jl.jlapp.eneity.PostModel;
import com.jl.jlapp.nets.Api;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.nets.Transformer;
import com.jl.jlapp.popu.ClearHistorySearhPopu;
import com.jl.jlapp.utils.NotificationsUtils;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.io.File;

import static android.webkit.WebSettings.LOAD_NO_CACHE;

public class MessageSettingsActivity
        extends Activity {
    private String TAG = "MessageSettingsActivityLog";
    @BindView(R.id.activity_message_setting_page)
    LinearLayout activityMessageSettingPage;
    ClearHistorySearhPopu clearHistorySearhPopu;
    @BindView(R.id.customer_service_webview)
    WebView customerServiceWebview;
    public OnClickListener itemclick = new OnClickListener() {
        public void onClick(View paramAnonymousView) {
            MessageSettingsActivity.this.buildProgressDialog();
            MessageSettingsActivity.this.clearHistorySearhPopu.dismiss();
            MessageSettingsActivity.this.deldectMessage(Integer.valueOf(MessageSettingsActivity.this.userId));
            MessageSettingsActivity.this.clearWebViewCache();
            MessageSettingsActivity.this.customerServiceWebview.loadUrl("javascript:deletelog()");
        }
    };
    boolean notificationEnabled = false;
    @BindView(R.id.notification_state)
    TextView notificationState;
    private ProgressDialog progressDialog;
    int userId = -1;
    String userPhone = "";

    public void buildProgressDialog() {
        if (this.progressDialog == null) {
            this.progressDialog = new ProgressDialog(this);
            this.progressDialog.setProgressStyle(0);
        }
        this.progressDialog.setMessage("清除中");
        this.progressDialog.setCancelable(true);
        this.progressDialog.show();
    }

    public void cancelProgressDialog() {
        if ((this.progressDialog != null) && (this.progressDialog.isShowing())) {
            this.progressDialog.dismiss();
        }
    }

    public void clearWebViewCache() {
        try {
            deleteDatabase("webview.db");
            deleteDatabase("webviewCache.db");
            this.customerServiceWebview.clearCache(true);
            this.customerServiceWebview.clearHistory();
            this.customerServiceWebview.clearFormData();
            File localFile = new File(getCacheDir().getAbsolutePath() + "/webviewCache");
            Log.e(this.TAG, "webviewCacheDir path=" + localFile.getAbsolutePath());
            if (localFile.exists()) {
                deleteFile(localFile);
            }
            return;
        } catch (Exception localException) {
        }
    }

    public void deldectMessage(Integer paramInteger) {
        Net.get().deldectMessage(paramInteger.intValue())
                .compose(Transformer.<PostModel>switchSchedulers())
                .subscribe(new CommonObserver<PostModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(PostModel postModel) {

                    }
                });
    }

    public void deleteFile(File paramFile) {
        Log.i(this.TAG, "delete file path=" + paramFile.getAbsolutePath());
        if (paramFile.exists()) {
            if (paramFile.isFile()) {
                paramFile.delete();
            } else {
                paramFile.delete();
                if (paramFile.isDirectory()) {
                    File[] arrayOfFile = paramFile.listFiles();
                    int i = 0;
                    while (i < arrayOfFile.length) {
                        deleteFile(arrayOfFile[i]);
                        i += 1;
                    }
                }
            }
        }
        Log.e(this.TAG, "delete file no exists " + paramFile.getAbsolutePath());
    }

    public void initWebView() {
        WebSettings localWebSettings = this.customerServiceWebview.getSettings();
        localWebSettings.setSaveFormData(false);
        localWebSettings.setLoadWithOverviewMode(true);
        localWebSettings.setJavaScriptEnabled(true);
        localWebSettings.setSupportZoom(true);
        localWebSettings.setUseWideViewPort(true);
        localWebSettings.setDomStorageEnabled(true);
        localWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        localWebSettings.setLoadWithOverviewMode(true);
        localWebSettings.setCacheMode(LOAD_NO_CACHE);
        this.customerServiceWebview.setWebViewClient(new WebViewClient());
        this.customerServiceWebview.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
        this.customerServiceWebview.loadUrl("http://117.158.178.202:8000/qiqiim-server/loginMoberl?account=" + this.userPhone);
    }

    @OnClick({R.id.icon_return, R.id.notification_state_rela, 2131230958})
    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.icon_return:
                finish();
                return;
            case R.id.notification_state_rela:
                requestPermission();
                return;
        }
        this.clearHistorySearhPopu = new ClearHistorySearhPopu(this, this.itemclick, Integer.valueOf(11));
        this.clearHistorySearhPopu.showAtLocation(findViewById(R.id.activity_message_setting_page), 17, 0, 0);
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_message_settings);
        ButterKnife.bind(this);
    }

    protected void onDestroy() {
        super.onDestroy();
        Log.d("aaaaaaaaaaaaaaaaaaaa", "我被销毁了");
        this.activityMessageSettingPage.removeView(this.customerServiceWebview);
        this.customerServiceWebview.destroy();
        this.customerServiceWebview.clearCache(true);
        this.customerServiceWebview.clearHistory();
        this.customerServiceWebview.clearFormData();
    }

    protected void onResume() {
        super.onResume();
        SharedPreferences localSharedPreferences = getSharedPreferences("JLAppData", 0);
        this.userId = localSharedPreferences.getInt("userId", -1);
        this.userPhone = localSharedPreferences.getString("mobile", "");
        if ((this.userId > 0) && (!"".equals(this.userPhone))) {
            initWebView();
            this.notificationEnabled = NotificationsUtils.isNotificationEnabled(this);
            Log.d(this.TAG, "通知栏权限状态:" + NotificationsUtils.isNotificationEnabled(this));
            if (this.notificationEnabled) {
                this.notificationState.setText("去设置");
                return;
            }
            this.notificationState.setText("去开启");
            return;
        }
        Toast.makeText(this, "您还未登录，请先登录。", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, LoginActivity.class));
    }

    protected void requestPermission() {
        if (Build.VERSION.SDK_INT >= 1) {
            startActivity(new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.parse("package:" + getPackageName())));
        }
        while (Build.VERSION.SDK_INT < 21) {
            return;
        }
        startActivity(new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.parse("package:" + getPackageName())));
    }

    final class DemoJavaScriptInterface {
        DemoJavaScriptInterface() {
        }

        @JavascriptInterface
        public void clickOnAndroid(String paramString) {
            Toast.makeText(MessageSettingsActivity.this, paramString, Toast.LENGTH_SHORT).show();
        }

        @JavascriptInterface
        public void hrefToChooseAddressPage() {
            Log.d("aaaaaaaaaaaaaaaaaa", "清除了");
        }

        @JavascriptInterface
        public void showToast(String paramString) {
            Toast.makeText(MessageSettingsActivity.this, paramString, Toast.LENGTH_LONG).show();
        }
    }

    final class MyWebChromeClient
            extends WebChromeClient {
        MyWebChromeClient() {
        }

        public boolean onJsAlert(WebView paramWebView, String paramString1, String paramString2, JsResult paramJsResult) {
            Log.d("webview", paramString2);
            paramJsResult.confirm();
            return true;
        }
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/MessageSettingsActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */